package jfxtras.application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Goran Lochert
 * Class that extends Application class and adds some basic functionality
 * that is needed in almost every JavaFX App, good for demos.
 */
public abstract class ExtendedApplicationX extends Application {

    protected double DEFAULT_APP_WIDTH = 500;
    protected double DEFAULT_APP_HEIGHT = 500;
    protected Stage stage;
    protected Pane root;
    protected Scene scene;

    @Override
    public void start(Stage primaryStage) {
        preSetup(primaryStage);
        setup();
        postSetup();
    }

    protected abstract void setup();

    protected void preSetup(Stage primaryStage) {
        stage = primaryStage;
        stage.setTitle(getAppTitle());
        root = new StackPane();
//        root.getStyleClass().add("root-pane"); // it is root by default since b40
        scene = new Scene(root, getAppWidth(), getAppHeight());
        stage.setScene(scene);
        setupCss();
    }

    protected void postSetup() {
        stage.centerOnScreen();
        stage.show();
    }

    // override to change APP WIDTH
    protected double getAppWidth() {
        return DEFAULT_APP_WIDTH;
    }

    // override to change APP HEIGHT
    protected double getAppHeight() {
        return DEFAULT_APP_HEIGHT;
    }

    // override to set App Title
    protected String getAppTitle() {
        return getClass().getSimpleName();
    }

    // override to add CSS styles
    protected void setupCss() {
    }

    public static void launchWithPrelaunch(java.lang.Class<? extends Application> appClass,
            java.lang.String[] args) {
        jfxtras.application.Application.prelaunch();
        launch(appClass, args);
    }
}
